/**
 * 
 *    Code contributed by IUP Research Institute
 * 
 *     This file is part of NetworkMon.
 *
 *   NetworkMon is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; version 2 of the License.
 *
 *   NetworkMon is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with NetworkMon; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package networkbackend;

/**
 * 
 * @author Brian Slocum
 */
import net.sourceforge.jpcap.capture.PacketCapture;
import net.sourceforge.jpcap.net.Packet;
import net.sourceforge.jpcap.net.RawPacket;

public class NetworkViewer implements
		net.sourceforge.jpcap.capture.RawPacketListener,
		net.sourceforge.jpcap.capture.PacketListener {

	// public event;
	protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList();
	private PacketCapture theViewer = new PacketCapture();
	private String[] allDevices;
	private DNSLinkedList DNSLookup = new DNSLinkedList(1024);

	/** Creates a new instance of NetworkViewer */
	public NetworkViewer() {
		// regester for events
		// theViewer.addRawPacketListener(this);
		theViewer.addPacketListener(this);

		// get all the devices for the computer
		try {
			allDevices = PacketCapture.lookupDevices();
			System.out.println("All Dev: = " + allDevices.length);

		} catch (net.sourceforge.jpcap.capture.CaptureDeviceLookupException ex) {
			System.out.println(ex);
		}
	}

	public String[] getAllDevices() {
		return allDevices;
	}

	public void StartGettingPackets(int deviceNumberToUse) {

		String theDevice = allDevices[deviceNumberToUse]; // you might need to
															// change this based
															// on your computers
															// setup

		// you have to do thos because Windows returns a "\n"
		// get the full name of the interface
		int cut;
		cut = theDevice.indexOf("\n");
		if ((cut) != -1) {
			theDevice = theDevice.substring(0, cut);
		}

		// open the connection
		try {
			theViewer.open(theDevice, true);
		} catch (net.sourceforge.jpcap.capture.CaptureDeviceOpenException ex) {
			System.out.println("Error Stage 2");
		}

		// start listing
		try {
			theViewer.capture(-1);
		} catch (net.sourceforge.jpcap.capture.CapturePacketException ex) {
			System.out.println("Error Stage 3");
		}

	}

	public void rawPacketArrived(RawPacket rawPacket) {
		System.out.println(rawPacket);
		}

	public void packetArrived(Packet packet) {
		APacket objectToRaise = new APacket(packet);
		// Let use call our Reverse DNS now.
		objectToRaise.setReceiversName(DNSLookup.reverseLookup(objectToRaise
				.getDestinationIPAddress()));
		objectToRaise.setSendersName(DNSLookup.reverseLookup(objectToRaise
				.getSourceIPAddress()));

		System.out.println(packet);
		System.out.println("Dest: " + objectToRaise.getDestinationIPAddress()
				+ " Src: " + objectToRaise.getSourceIPAddress() + " DestPort: "
				+ objectToRaise.getDestinationPortNumber() + " SrcPort: "
				+ objectToRaise.getSourcePortNumber() + " Protoc: "
				+ objectToRaise.getProtocall());
		fireTheEvent(objectToRaise);
	}

	public void StopGettingPackets() {
		theViewer.close();
	}

	// <editor-fold defaultstate="collapsed" desc=" Event Stuffs ">

	public void addthePacketListener(PacketEventListenerInterface listener) {
		listenerList.add(PacketEventListenerInterface.class, listener);
	}

	public void removethePacketListener(PacketEventListenerInterface listener) {
		listenerList.remove(PacketEventListenerInterface.class, listener);
	}

	void fireTheEvent(Object evt) {
		Object[] listeners = listenerList.getListenerList();
		// Each listener occupies two elements - the first is the listener class
		// and the second is the listener instance
		for (int i = 0; i < listeners.length; i += 2) {
			if (listeners[i] == PacketEventListenerInterface.class) {
				((PacketEventListenerInterface) listeners[i + 1])
						.APacketHasBeenCapturedOccurred(evt);
			}
		}
	} // </editor-fold>
}
